import { Module } from '@nestjs/common';
import { ConfigModule } from "@nestjs/config";
import { TypeOrmModule } from '@nestjs/typeorm';
import { WinstonModule } from 'nest-winston';
import Configs from "src/config/index";
import { MessageModule } from "src/message/message.module";
import { PaginationModule } from "src/utils/pagination/pagination.module";
import { HelperModule } from "src/utils/helper/helper.module";
import { MiddlewareModule } from "src/utils/middleware/middleware.module";
import { DatabaseModule } from "src/database/database.module";
import { RequestModule } from "src/utils/request/request.module";
import { ErrorModule } from "src/utils/error/error.module";
import { DatabaseService } from 'src/database/service/database.service';
import { AppRouterModule } from 'src/modules/router.module';
import { DebuggerOptionService } from './debugger/service/debugger.option.service';
import { DebuggerModule } from './debugger/debugger.module';
import { AuthModule } from './auth/auth.module';
import { CacheModule } from './utils/cache/cache.module';

// 根据环境变量，设置加载的配置文件
const isProd = process.env.NODE_ENV === 'production';
const envFiles = isProd ? ['.env', '.env.production'] : ['.env', '.env.development']

@Module({
  controllers: [],
  providers: [],
  imports: [
    MiddlewareModule,
    ConfigModule.forRoot({
      load: Configs,
      ignoreEnvFile: false,
      isGlobal: true,
      cache: true,
      envFilePath: envFiles,
    }),
    WinstonModule.forRootAsync({
      inject: [DebuggerOptionService],
      imports: [DebuggerModule],
      useFactory: (loggerService: DebuggerOptionService) => loggerService.createLogger(),
    }),
    TypeOrmModule.forRootAsync({
      imports: [DatabaseModule],
      inject: [DatabaseService],
      useFactory: (databaseService: DatabaseService) => databaseService.createTypeOrmOptions(),
    }),
    ErrorModule,
    DatabaseModule,
    MessageModule,
    PaginationModule,
    RequestModule,
    HelperModule,
    AppRouterModule,
    AuthModule,
    CacheModule
  ],
})
export class AppModule {}
